﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Xml;

using KELT.Viewport;

using KELT;
using KELT.KELTObjects.LibraryObjectModel;

namespace KELT.KELTObjects.ViewportObjectModel
{
    public class PictureObject : ViewportObj
    {
        
        private Image frontImageObject;
        private Image backImageObject;


        public PictureObject(TOOL mainContext, double x, double y)
        {
            ObjectInfo = "PictureObject";

            double changeX = (x * 2 / mainContext.mainViewport.RenderSize.Width - 1) * (0.4 * mainContext.cameraManager.getPoint3D().Z) + mainContext.cameraManager.getPoint3D().X + mainContext.cameraManager.getTranslateTransform().OffsetX;
            double changeY = (y * -2 / mainContext.mainViewport.RenderSize.Height + 1) * (0.24 * mainContext.cameraManager.getPoint3D().Z) + mainContext.cameraManager.getPoint3D().Y + mainContext.cameraManager.getTranslateTransform().OffsetY;
            indexNumber = mainContext.viewPortMgr.INDEXNUMBER;
            objectName = ObjectInfo + indexNumber;

            frontImageObject = new Image();
            backImageObject = new Image();

            point = new Point3D(changeX, changeY, 0);

            frontViewBox = new Viewbox();
            frontViewBox.Name = "object" + indexNumber;

            FrontViusal = new Viewport2DVisual3D();
            
            colorBrush = new SolidColorBrush();

            frontViewBox.Child = frontImageObject;
            
            colorBrush.Color = Brushes.White.Color;
            frontImageObject.OpacityMask = colorBrush;

            FrontViusal = CreateFront(frontViewBox, FrontTransform3DGroup);

            CreateBack(mainContext);

            BackViewBox.Child = backImageObject;

            frontViewBox.MouseDown += new MouseButtonEventHandler(mainContext.Object_MouseDown);
            frontViewBox.MouseMove += new MouseEventHandler(mainContext.Object_MouseMove);
            frontViewBox.MouseRightButtonDown += new MouseButtonEventHandler(mainContext.EncoderWindow_MouseRightButtonDown);

            isVisual = true;
            mainContext.mainViewport.Children.Add(FrontViusal);

            mainContext.RegisterName(AnimationInfo.ANIMATION_ALPHA + indexNumber, colorBrush);
            mainContext.RegisterName(AnimationInfo.ANIMATION_TRANSLATE + indexNumber, FrontTransform3DGroup.Children.ElementAt(0));
            mainContext.RegisterName(AnimationInfo.ANIMATION_SCALE + indexNumber, FrontTransform3DGroup.Children.ElementAt(1));
            mainContext.RegisterName(AnimationInfo.ANIMATION_ROTATE + indexNumber, FrontTransform3DGroup.Children.ElementAt(2));
        }


        public void setImage(string str)
        {
            BitmapImage bitmap = new BitmapImage(new System.Uri(str));

            width = (int)bitmap.Width / 60;
            height = (int)bitmap.Height / 60;
            frontImageObject.Source = bitmap;
            backImageObject.Source = bitmap;
            m_ImgSrc = str;
            setSize();
        }
        public void setSize()
        {
            FrontPoint3DCollection[0] = new Point3D((double)-width / 1, height, 0);
            FrontPoint3DCollection[1] = new Point3D((double)-width / 1, -height, 0);
            FrontPoint3DCollection[2] = new Point3D((double)width / 1, -height, 0);
            FrontPoint3DCollection[3] = new Point3D((double)width / 1, height, 0);


            BackPoint3DCollection[0] = new Point3D((double)-width / 1, height, 0);
            BackPoint3DCollection[1] = new Point3D((double)-width / 1, -height, 0);
            BackPoint3DCollection[2] = new Point3D((double)width / 1, -height, 0);
            BackPoint3DCollection[3] = new Point3D((double)width / 1, height, 0);



        }
        

        protected string m_ImgSrc = null;
        public string imgSrc
        {
            get
            {
                if (m_ImgSrc == null)
                {
                    if (this.LibraryObj != null)
                    {
                        m_ImgSrc = (this.LibraryObj as MultimediaObj).fileFullPath;
                        return (this.LibraryObj as MultimediaObj).fileFullPath;
                    }
                    else
                    {
                        return "Untitled Object";
                    }
                }
                else
                {
                    return m_ImgSrc;
                }

            }
            set
            {
                if ((this.LibraryObj as MultimediaObj) != null)
                {
                    (this.LibraryObj as MultimediaObj).fileFullPath = value;
                    setImage(value);
                }
            }
        }



        #region XML
        public override void GetObjInfoXML(XmlWriter writer)
        {
            writer.WriteStartElement("PictureObject");
            {
                //writer.WriteElementString("fileSource", );
            }
            writer.WriteEndElement();
        }
        #endregion
    }
}
